package org.example.elastic.lite.config;

import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 动态定时任务配置
 * @author 魏荣杰
 * @date 2022/11/11 09:54
 * @since
 * @version
 */
@Configuration
public class DynamicElasticJobConfig {

    @Value("${elasticjob.reg-center.server-lists}")
    private String serverLists;
    @Value("${elasticjob.reg-center.namespace}")
    private String namespace;
    @Resource
    private DataSource dataSource;

    @Bean
    public ZookeeperConfiguration zookeeperConfiguration() {
        return new ZookeeperConfiguration(serverLists, namespace);
    }

    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter zookeeperRegistryCenter(ZookeeperConfiguration zookeeperConfiguration) {
        return new ZookeeperRegistryCenter(zookeeperConfiguration);
    }

    @Bean
    public DynamicElasticJobListener elasticJobListener() {
        return new DynamicElasticJobListener(100, 100);
    }

    @Bean
    public JobEventConfiguration jobEventConfiguration() {
        return new JobEventRdbConfiguration(dataSource);
    }
}
